package theFirstWay;

import java.util.Random;

public class ThreadPush implements Runnable{
    public int flag = 1;
    Random random;
    int i =0;

    public ThreadPush(){
        random = new Random();
    }

    @Override
    public void run() {

            do {
                System.out.println(i+"ThreadPush Start:");
                while (flag != MainClass.sysFlag) {
//                  busy waiting
                }
//        critical section start , 产生随机数并入栈
                int a = random.nextInt(100);
                MainClass.stack.push(a);
                System.out.println(i+"已把随机数" + a + " 入栈");
//        critical section end,让出临界区
                MainClass.sysFlag = 0;
                i++;
            } while (i < 1000);
    }
}
